package fr.grassaud.hmvc.sample;

import fr.grassaud.hmvc.component.Child;
import fr.grassaud.hmvc.component.ControlledModel;
import fr.grassaud.hmvc.component.ControlledView;
import fr.grassaud.hmvc.component.Controller;
import fr.grassaud.hmvc.event.HMVCEvent;
import fr.grassaud.hmvc.event.OnControllerEvent;
import fr.grassaud.hmvc.event.OnInit;
import fr.grassaud.hmvc.event.OnModelEvent;
import fr.grassaud.hmvc.event.OnViewEvent;


@Controller
public class SampleController {

    @ControlledView
    protected SampleView view;
    
    @ControlledModel
    protected SampleModel model;
    
    @Child(name="mvc-child")
    private ChildController child;
    
    
    
    
    @OnInit
    private void init() {
	model.setData("data initialized");
    }
    
    @OnViewEvent(name="getModel")
    public void receiveViewEvent(HMVCEvent event) {
	this.model.setViewEvent("OK");
    }
    
    @OnModelEvent(name="load")
    public void receiveModelEvent(HMVCEvent event) {
	this.model.setModelEvent("OK");
    }
    
    @OnControllerEvent(name="displayDetail")
    public void receiveControllerEvent(HMVCEvent event ) {
	this.model.setControllerEvent("OK");
    }
    
    
    public ChildController getChild() {
	return this.child;
    }
    
    
    @OnViewEvent(name=ChildEvent.NAME)
    public void createdynamicChild(@Child(name="mvc-dyn") ChildController controller,ChildEvent event) {
	controller.changeData(event);
    }
    
    
    @OnModelEvent(name=ChildEvent.NAME)
    public void onDynamicEvent(ChildEvent event) {
	model.setData(event.getData());
    }
    
}
